#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
 
void enqueue(struct queue *q, void* value) {
	struct queue_node *node = malloc(sizeof(struct queue_node));
	if (node == NULL) {
		fprintf(stderr, "could not insert into queue, aborting\n");
		exit(-1);
	}
	node->data = value;
	if (q->first == NULL)
		q->first = q->last = node;
	else {
		q->last->next = node;
		q->last = node;
	}
	node->next = NULL;
}

void* dequeue(struct queue *q) {

    if (!q->first)
        return NULL;
    
    struct queue_node *tmp = q->first;
		void* value = q->first->data;
    if (q->first == q->last)
        q->first = q->last = NULL;
    else
        q->first = q->first->next;
 
    free(tmp);
    return value;
}

void init_queue(struct queue *q) {
	q->first = q->last = NULL;
}

int queue_empty(struct queue *q) {
	return q->first == NULL;
}
